package com.lg.aptcomp.handlers

import android.util.Log
import android.widget.Toast
import com.lg.aptcomp.SecondFragment
import com.lg.aptcomp.model.LoginFormModel
import com.lg.aptcomp.model.LoginResponseModel
import com.lg.aptcomp.network.ApiConnect
import com.lg.common.network.ApiCustomCallback
import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import io.reactivex.schedulers.Schedulers.io

open class LoginHandler(val mFragmentContext: SecondFragment){
   val TAG : String = "LoginHandler"
    fun onClickCancelBtn() {

    }

    fun onClickForgotPassword(email: String) {
    }


    fun onClickLogin(loginmodel : LoginFormModel?){
        Log.d(TAG,"onClickLogin")
        try {
            Log.d(TAG,mFragmentContext.toString())
            Log.d(TAG,loginmodel.toString())

            if(loginmodel!!.isFormValidated(mFragmentContext)){
                Log.d(TAG,"isFormValidated")

                ApiConnect.login(mFragmentContext.requireContext()!!,loginmodel!!).
                observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(object :ApiCustomCallback<LoginResponseModel>(mFragmentContext.requireContext(),true){

                        override fun onNext(responseModel: LoginResponseModel) {

                            super.onNext(responseModel)
                            mFragmentContext.mContentViewBinding.loading = false
                            Log.d(TAG,"responseModel.errorCode="+responseModel.errorCode)

                            if(responseModel.errorCode==0){
                                Toast.makeText(context,"success",Toast.LENGTH_SHORT).show()

                            }else{
                                Toast.makeText(context,responseModel.errorMsg,Toast.LENGTH_LONG).show()

                            }

                        }

                        override fun onError(e: Throwable) {
                            super.onError(e)
                            mFragmentContext.mContentViewBinding.loading = false
                            Toast.makeText(context,"error",Toast.LENGTH_SHORT).show()


                        }
                    });
            }
        }catch (e:Exception){
           e.printStackTrace()
        }




    }

    fun onClickSignUp(){

    }

    }